package one.coder.httpclient;

import java.io.IOException;

import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

/**
 * @author lihzh
 * @date 2012-6-12 上午10:24:26
 * @blog (One Coder)http://www.coderli.com
 */
public class HttpClientTimeOutTest {

	/**
	 * 测试超市情景
	 * 
	 * @author lihzh
	 * @date 2012-6-12 下午4:14:45
	 */
	@Test
	public void SingleRequestWithProxyTest() {
		HttpClient client = new DefaultHttpClient();
		int count = 1;
		while (true) {
			doGet(client, true);
			System.out.println("Request: " + count++);
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 测试并发访问，需要用到GroboUtils。通过代理
	 * 
	 * @author lihzh
	 * @date 2012-6-12 下午5:43:00
	 */
	@Test
	public void MultiRequestsWithProxyTest() {
		PoolingClientConnectionManager connManager = new PoolingClientConnectionManager();
		HttpParams params = new BasicHttpParams();
		params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 100);
		final HttpClient client = new DefaultHttpClient(connManager, params);
		TestRunnable runner = new TestRunnable() {
			@Override
			public void runTest() throws Throwable {
				System.out.println(doGet(client, true));
			}
		};
		int runnerCount = 100;
		TestRunnable[] trs = new TestRunnable[runnerCount];
		for (int i = 0; i < runnerCount; i++) {
			trs[i] = runner;
		}
		MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
		try {
			mttr.runTestRunnables();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	/**
	 * 多线程并发访问，不通过代理
	 * 
	 * @author lihzh
	 * @date 2012-6-12 下午9:18:11
	 */
	@Test
	public void MultiRequestsTest() {
		PoolingClientConnectionManager connManager = new PoolingClientConnectionManager();
		final HttpClient client = new DefaultHttpClient(connManager);
		TestRunnable runner = new TestRunnable() {
			@Override
			public void runTest() throws Throwable {
				System.out.println(doGet(client, false));
			}
		};
		int runnerCount = 100;
		TestRunnable[] trs = new TestRunnable[runnerCount];
		for (int i = 0; i < runnerCount; i++) {
			trs[i] = runner;
		}
		MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
		try {
			mttr.runTestRunnables();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	private String doGet(HttpClient client, boolean useProxy) {
		if (useProxy) {
			HttpHost proxy = new HttpHost("127.0.0.1", 8888);
			client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
		}
		HttpGet get = new HttpGet("http://www.baidu.com");
		try {
			HttpResponse response = client.execute(get);
			return EntityUtils.toString(response.getEntity());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			get.abort();
		}
		return null;
	}

}
